package com.hd.trans.files.adapter;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

import com.hd.trans.R;
import com.hd.trans.files.bean.Audio;
import com.hd.trans.files.holder.MediaPathViewHolder;
import com.hd.trans.files.sort.ISort;
import com.hd.trans.files.sort.MediaSortHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过本地路径查找音频文件
 */
public class AudioPathAdapter extends BaseAdapter<Audio, MediaPathViewHolder> {

    private List<Audio> mSelectedList = new ArrayList<>();

    public AudioPathAdapter() {
        super(new DiffUtil.ItemCallback<Audio>() {
            @Override
            public boolean areItemsTheSame(@NonNull Audio oldItem, @NonNull Audio newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull Audio oldItem, @NonNull Audio newItem) {
                return false;
            }
        });
        addChildClickViewIds(R.id.file_path_cb);
    }

    @Override
    MediaPathViewHolder createViewHolder(View itemView) {
        return new MediaPathViewHolder(itemView);
    }

    @Override
    int getLayoutId() {
        return R.layout.layout_item_files_path;
    }

    @Override
    void bindViewHolder(MediaPathViewHolder holder, Audio audio) {
        holder.setText(R.id.file_path_tv_name, audio.getDisplay_name());
        holder.setImageRes(R.id.file_path_iv_icon, audio.getImageRes());
        holder.showCheckBox(audio, mSelectedList);
        holder.showAudioExtraInfo(audio);
    }

    public void updateSelectedAudios(List<Audio> audios) {
        this.mSelectedList = audios;
        notifyDataSetChanged();
    }

    /**
     * 根据排序规则进行排序
     *
     * @param method 排序规则
     */
    public void sort(ISort.SortMethod method) {
        List<Audio> sourceAudios = new ArrayList<>(getCurrentList());
        submitList(MediaSortHelper.getInstance().sortAudio(sourceAudios, method));
    }
}
